<?php
	
	class wallDB extends SQLite3
	{
		public static $instance;
		
		public static function getInstance()
		{
			if (self::$instance==null)
				self::$instance = new wallDB();
				
			return self::$instance;
		}
		
		function __construct()
	    {
	        global $CurrentUserId;
	        
	        if ($CurrentUserId==0 || $CurrentUserId=="" || $CurrentUserId==null)
	        	die("no User Found");
	        
	        $dbfile = 'Profiles\\u'.$CurrentUserId.'_Wall.sqlite';
	        
	        $dbNeedsCreation = !file_exists($dbfile);
	        
	        $this->open($dbfile);
	        
	        $this->exec("PRAGMA cache_size=200000");
	        $this->exec("PRAGMA synchronous=OFF");
	        $this->exec("PRAGMA journal_mode=MEMORY");
	        $this->exec("PRAGMA temp_store=MEMORY");
	        $this->exec("vacuum");
	        
	        //chk if we need to create DB
	        if ($dbNeedsCreation)
	        {
	        	$this->createTables();
	        	
	        	//good place to make dir
	        	$fileDumpsBase = "tmp_dir/Wall2/";
	        	if (!file_exists($fileDumpsBase))
	        		mkdir($fileDumpsBase);
	        		
	        	//parse XML
	        	wallMaintainance::parseXML();
	        }
	    }
	    
	    function getParsedVersion()
	    {
	    	return $this->getSetting('parsedVersion');
	    }
	    
	    function setParsedVersion($newVer)
	    {
	    	$this->setSetting('parsedVersion', $newVer);
	    }
	    
	    function getSetting($setting)
	    {
	    	$res = $this->query("SELECT val FROM settings WHERE setting='$setting'");
	    		    		
	    	$dt = $res->fetchArray(SQLITE3_ASSOC);
	    	return $dt["val"];
	    }
	    
	    function setSetting($setting, $val)
	    {
	    	$ssql = "REPLACE INTO settings values ('$setting','$val')";
	    	$this->query($ssql);
	    }
	    
	    function getAllVirals()
	    {
	    	$ssql = "SELECT id FROM virals ORDER BY id";
	    	$res = $this->query($ssql);
	    	
	    	$arRet = array();
	    	while($dt = $res->fetchArray(SQLITE3_ASSOC))
	    		array_push($arRet, $dt["id"]);
	    	
	    	return $arRet;
	    }
	    
	    function getMyRequests($idsOnly)
	    {
	    	$ssql = ($idsOnly)?
	    			"SELECT id FROM myrequests":
	    			"SELECT * FROM myrequests"; 
	    		
	    	$res = $this->query($ssql);
	    	
	    	$arRet = array();
	    	while($dt = $res->fetchArray(SQLITE3_ASSOC))
	    	{
	    		if ($idsOnly)
	    			array_push($arRet, $dt["id"]);
	    		else
	    			array_push($arRet, $dt);
	    	}
	    	
	    	return $arRet;
	    }
	    
	    function getActiveRequestsCount()
	    {
	    	$res = $this->query("SELECT COUNT(id) cCount FROM myrequests");
	    	$dt = $res->fetchArray(SQLITE3_ASSOC);
	    	
	    	return $dt["cCount"];
	    }
	    
		function getViralsCount()
	    {
	    	$res = $this->query("SELECT COUNT(id) cCount FROM virals");
	    	$dt = $res->fetchArray(SQLITE3_ASSOC);
	    	
	    	return $dt["cCount"];
	    }
	    
	    function getExpTimeForId($id)
	    {
	    	$res = $this->query("SELECT timeTillReset FROM virals WHERE id='$id' LIMIT 1");
	    		
	    	$dt = $res->fetchArray(SQLITE3_ASSOC);
	    	return $dt["timeTillReset"];
	    }
	    
	    function createTables()
	    {
	    	$ssql = 'CREATE TABLE if not exists [virals] 
	    				([id] VARCHAR(50)  NULL, 
	    				[timeTillReset] INTEGER(2) NULL, 
	    				[maxHostRewards] INTEGER(2) NULL,
	    				[rewardexpirationTime] INTEGER(2) NULL,
	    				[rewardmaxHelpers] INTEGER(2) NULL,
	    				[imPath] VARCHAR(255) NULL,
	    				[hostRewardsType] VARCHAR(50) NULL, 
	    				[hostRewardsAmount] INTEGER(2) NULL, 
	    				[helperRewardsType] VARCHAR(50) NULL, 
	    				[helperRewardsAmount] INTEGER(2) NULL, 
	    				[revision] INTERGER(10) NULL,
	    				UNIQUE (id))';
	    	$this->exec($ssql);
	    	
	    	$ssql = "CREATE TABLE if not exists [myrequests] (
						[id] vaRCHAR(50)  PRIMARY KEY NULL,
						[lastWas] INTEGER(10) DEFAULT 0 NULL,
						[nextWill] INTEGER(50) DEFAULT 0 NULL,
						[postToFile] INTEGER(1) DEFAULT 1 NULL,
						[postToCVEx] INTEGER(1) DEFAULT 0 NULL,
						[postToCVLinkEx] INTEGER(1) DEFAULT 0 NULL,
						[reqCount] INTEGER(10) DEFAULT 0 NULL,
						[maxWanted] INTEGER(10) DEFAULT 0 NULL
					 )";
	    	$this->exec($ssql);
	    	
	    	$ssql = "CREATE TABLE if not exists [settings] (
						[setting] VARCHAR(50)  PRIMARY KEY NULL,
						[val] VARCHAR(50)  NULL
					)";
	    	$this->exec($ssql);
	    	$this->exec("REPLACE INTO settings values ('parsedVersion',0)");
	    	$this->exec("REPLACE INTO settings values ('postToFile',1)");
	    	$this->exec("REPLACE INTO settings values ('postToCVEx',1)");
	    	$this->exec("REPLACE INTO settings values ('postToCVLinkEx',0)");
	    	$this->exec("REPLACE INTO settings values ('totalLinksCreated',0)");
	    	$this->exec("REPLACE INTO settings values ('lastDateChecked',0)");
	    	$this->exec("REPLACE INTO settings values ('showImages',0)");
	    	
	    }
	}

?>